package com.atguigu.atcrowdfunding.component;

import com.atguigu.atcrowdfunding.bean.TAdmin;
import com.atguigu.atcrowdfunding.bean.TAdminExample;
import com.atguigu.atcrowdfunding.bean.TPermission;
import com.atguigu.atcrowdfunding.bean.TRole;
import com.atguigu.atcrowdfunding.mapper.TAdminMapper;
import com.atguigu.atcrowdfunding.mapper.TPermissionMapper;
import com.atguigu.atcrowdfunding.mapper.TRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class SecurityUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    TAdminMapper adminMapper;
    @Autowired
    TRoleMapper roleMapper;
    @Autowired
    TPermissionMapper permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Logger logger = LoggerFactory.getLogger(getClass());
        //1.查询用户对象
        TAdminExample example = new TAdminExample();
        example.createCriteria().andLoginacctEqualTo(username);
        List<TAdmin> list = adminMapper.selectByExample(example);
        if (list != null && list.size()==1) {
            TAdmin admin = list.get(0);
            Integer adminId = admin.getId();
            logger.debug("用户信息:{}",admin);

            //2.将这个用户对应的所有角色和权限全部查询出来
            List<TRole> roleList = roleMapper.listRoleByAdminId(adminId);


         //3.查询权限集合
           List<TPermission> permissionList = permissionMapper.listPermissionByAdminId(adminId);
            //4.构建用户所有权限集合=>（ROLE_角色+权限）
            Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
            for (TRole role:roleList) {
                authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
            }

            for (TPermission permission:permissionList) {
                authorities.add(new SimpleGrantedAuthority(permission.getName()));
            }


            return new User(admin.getLoginacct(),admin.getUserpswd(),authorities);
        } else {
            return null;
        }

    }
}
